﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web;
using Icst.Leaflike.Logging;

namespace Icst.Leaflike.Web
{
    public static class PageExtension
    {
        public static int UserCount(this HttpApplicationState Application)
        {
            Application.Lock();
            int count = 0;
            try
            {
                count = int.Parse(Application["UserCount"].ToString());
            }
            catch (Exception e)
            {
                Logger.Warning("FAILED TO PARSE USER COUNT." + e.Message);
            }
            finally
            {
                Application.UnLock();
            }

            return count;
        }

        public static void IncUserCount(this HttpApplicationState Application)
        {
            Application.Lock();
            try
            {
                int count = int.Parse(Application["UserCount"].ToString());
                Application["UserCount"] = count + 1;
            }
            catch (Exception e)
            {
                Logger.Warning("FAILED TO IncUserCount." + e.Message);
            }
            finally
            {
                Application.UnLock();
            }
        }

        public static void DecUserCount(this HttpApplicationState Application)
        {
            Application.Lock();
            try
            {
                int count = int.Parse(Application["UserCount"].ToString());
                Application["UserCount"] = count - 1;
            }
            catch (Exception e)
            {
                Logger.Warning("FAILED TO DecUserCount." + e.Message);
            }
            finally
            {
                Application.UnLock();
            }
        }

        public static void ClearUserCount(this HttpApplicationState Application)
        {
            Application.Lock();
            try
            {
                Application["UserCount"] = 0;
            }
            catch (Exception e)
            {
                Logger.Warning("FAILED TO ClearUserCount." + e.Message);
            }
            finally
            {
                Application.UnLock();
            }
        }
        public static void Redirect(this Page page, string url)
        {
            string script = string.Format("<script language=\"javascript\">window.top.location.href='{0}';</script>",url);
            page.Response.Write(script);
        }

        public static string Para(this Page page, string key)
        {
            if (string.IsNullOrEmpty(page.Request[key]))
                return string.Empty;
            return page.Request[key];
        }

        public static string Para(this Page page, string key, string defaultValue)
        {
            if (string.IsNullOrEmpty(page.Request[key]))
                return defaultValue;
            return page.Request[key];
        }

        public static int ParaInt(this Page page, string key)
        {
            if (string.IsNullOrEmpty(page.Request[key]))
                return 0;
            return int.Parse(page.Request[key]);
        }

        public static int ParaInt(this Page page, string key, int defaultValue)
        {
            if (string.IsNullOrEmpty(page.Request[key]))
                return defaultValue;
            return int.Parse(page.Request[key]);
        }
    }
}
